package br.brpv.silvestrin.vendas.util;

import java.util.Calendar;

import br.brpv.silvestrin.vendas.R;

import android.app.TimePickerDialog;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;

public class TextViewTimePicker extends TextView {
    private int mHour;
    private int mMinute;

	public TextViewTimePicker(Context context, AttributeSet attrs) {
		super(context, attrs);
		initTime();
		updateText();
		
		setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				TimePickerDialog picker = new TimePickerDialog(getContext(), mTimeSetListener, mHour, mMinute, true);
				picker.setTitle(R.string.date_picker_title);
				picker.show();
			}
		});
	}
	
	public String getTime() {
		return (mHour < 10? "0" + mHour: mHour) + ":" + (mMinute < 10? "0" + mMinute: mMinute);
	}

    public void setTime(int hour, int minute) {
        mHour = hour;
        mMinute = minute;
        updateText();
    }
	
	private void initTime() {
		Calendar c = Calendar.getInstance();
		mHour = c.get(Calendar.HOUR_OF_DAY);
		mMinute = c.get(Calendar.MINUTE);
	}
	
	private void updateText() {
		setText((mHour < 10? "0" + mHour: mHour) + " : " + (mMinute < 10? "0" + mMinute: mMinute));
	}
	
	private TimePickerDialog.OnTimeSetListener mTimeSetListener =
		    new TimePickerDialog.OnTimeSetListener() {
		        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
		            mHour = hourOfDay;
		            mMinute = minute;
		            updateText();
		        }
		    };
}
